let formatTime = date => {
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let minute = date.getMinutes()
    let second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}




let formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}

let dateConvert = timestamp => {
    let date = new Date(timestamp)
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let minute = date.getMinutes()
    let second = date.getSeconds()

    return formatNumber(year) + '年' + formatNumber(month) + '月' + formatNumber(day) + '日' + [hour, minute, second].map(formatNumber).join(':')
}


let leftTimer = leftTime => {
    let dd = parseInt(leftTime / 60 / 60 / 24, 10);//计算剩余的天数
    let hh = parseInt(leftTime / 60 / 60 % 24, 10);//计算剩余的小时数
    let mm = parseInt(leftTime / 60 % 60, 10);//计算剩余的分钟数
    let ss = parseInt(leftTime % 60, 10);//计算剩余的秒数
    dd = formatNumber(dd);
    hh = formatNumber(hh);
    mm = formatNumber(mm);
    ss = formatNumber(ss);//小于10的话加0
    let left = {
        day: dd,
        hour: hh,
        minute: mm,
        seconds: ss
    };
    return left;
}

let isNumber = obj => {
    return typeof obj === 'number' && !isNaN(obj)
}

/**
 * 数组中的对象去重
 */
function obj2Key(obj, keys) {
    var n = keys.length;
    var key = [];
    while (n--) {
        key.push(obj[keys[n]]);
    }
    return key.join('|');
}
function uniqeByKeys(array, keys) {
    var arr = [];
    var hash = {};
    for (var i = 0, j = array.length; i < j; i++) {
        var k = obj2Key(array[i], keys);
        if (!(k in hash)) {
            hash[k] = true;
            arr.push(array[i]);
        }
    }
    return arr;
}

// 检测是否带有token 没有就去跳转验证
let checkToken = () => {
    if (getApp().globalData.token == undefined || getApp().globalData.token == null || getApp().globalData.token == '') {
        wx.navigateTo({
            url: '/pages/auth/auth',
        })
    } else {
        console.log("检测到Token值----------------" + getApp().globalData.token)
    }
}
/**
    * 倒计时
    */
function countdown(enddate) {
    let self = this;
    let leftTime = (new Date(enddate)) - new Date(); //计算剩余的毫秒数
    let days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
    let hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时
    let minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟
    let seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数
    days = checkTime(days);
    hours = checkTime(hours);
    minutes = checkTime(minutes);
    seconds = checkTime(seconds);
    let countdown = {
        day: days,
        hour: hours,
        minute: minutes,
        seconds: seconds
    };
    return countdown;
}
 function checkTime(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}


module.exports = {
    formatTime: formatTime,
    leftTimer: leftTimer,
    dateConvert: dateConvert,
    obj2Key: obj2Key,
    uniqeByKeys: uniqeByKeys,
    isNumber: isNumber,
    checkToken: checkToken,
    countdown: countdown,
}
